package com.board.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.jwt.JWTUtil;
import com.board.dto.StoreDto;
import com.board.dto.StoreInfoDto;
import com.board.entity.StoreInfo;
import com.board.form.*;
import com.board.param.*;
import com.board.service.StoreInfoService;

import com.board.util.JwtUtil;
import com.board.util.RedisUtil;
import com.board.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LGL
 * @since 2024年04月03日
 */
@RestController
@RequestMapping("/storeInfo")
@Api(tags = "商家信息相关接口")
public class StoreInfoController {
    @Autowired
    private StoreInfoService storeInfoService;

    //根据id查询商家信息
    @GetMapping("/findStoreById")
    public Result<StoreDto> findStoreById(Integer storeId){
        StoreDto dto= storeInfoService.findStoreById(storeId);
        return Result.success("查询成功",dto);
    }

    //商家登录
    @PostMapping("/login")
    @ApiOperation("商家账号密码登录")
    public Result login(@RequestBody StoreLoginForm form, HttpServletResponse response){
        StoreInfoDto storeInfo =  storeInfoService.login(BeanUtil.toBean(form,StoreLoginParam.class));
        //登录成功，生成JWT令牌
        HashMap<String, Object> map = new HashMap<>();
        map.put("store_id",storeInfo.getStoreId());
        map.put("role","商家");
        String accessToken = JwtUtil.createToken(map,60);
        String refreshToken = JwtUtil.createToken(map, 24 * 60);
//        redisUtil.setString(accessToken,refreshToken,24*60*60);
        response.setHeader("token",accessToken);
        //在跨域请求访问时 必须公开该请求头 前端才能获取到
        response.setHeader("Access-Control-Expose-Headers","token");
        return Result.success("登录成功",storeInfo);
    }


}

